/* 
 * osc_send_gui
 */

// Note if only sending messages see oscP5flush

import oscP5.*;
import netP5.*;
import controlP5.*;

ControlP5 cp5;
OscP5 oscP5;


NetAddress naServerAddr;

int srvrSock = 9001;

Textfield gTextfield;


void setup() {
  size(400,400);
 
  oscP5 = new OscP5(this,srvrSock);
  naServerAddr = new NetAddress("127.0.0.1", srvrSock); 
  
  
  PFont font = createFont("arial",20);
  
  cp5 = new ControlP5(this);

  // Note order of addition is important. The bang buttons
  // depend on gTextfield so gTextfield must be initialized first.
  gTextfield = cp5.addTextfield("textinput")
                  .setPosition(100, 200)
                  .setSize(200, 20)
                  .setFocus(true)
                  .setCaptionLabel("Integer");
                  ;
                  
  // setInputFilter returns void so it cannot be chained like setFocus etc.                  
  gTextfield.setInputFilter(ControlP5.INTEGER);                
    // create a new button with name 'buttonA'
  cp5.addButton("bang1")
     .setValue(0)
     .setPosition(100,100)
     .setSize(35,35)
     .setCaptionLabel("send")
     ;

    // create a new button with name 'buttonA'
  cp5.addButton("bang2")
     .setValue(255)
     .setPosition(150,100)
     .setSize(35,35)
     .setCaptionLabel("clear")
     ;
     


}

void draw() {
  background(176,196,222);  
}

// Button 'bang1' callback...
void bang1(int bangVal) {
  String ss = gTextfield.getText();
  if(ss.length() > 0) {
    OscMessage msg = new OscMessage("/test");
    msg.add(ss);
    oscP5.flush(msg,naServerAddr); 
  }
}

// Button 'bang2' callback...
void bang2(int bangVal) {
  gTextfield.clear();
}

